﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StockTrader.DAL.HnxMonitor.Config;
using System.Configuration;

namespace StockTrader.DAL.HnxMonitor
{
    public partial class frmOptions : SlideDialog.SlideDialog
    {
        public frmOptions(Form poOwner, float pfStep)
            : base(poOwner, pfStep)
        {
            InitializeComponent();
            for (int i = 0; i < clObj.Items.Count; i++)
            {
                if (Settings.UPDATE_LIST.Contains(clObj.Items[i].ToString()))
                    clObj.SetItemChecked(i, true);
            }
            txtSpeed.Text = Settings.TRADING_INTERVAL.ToString();
            dtBoD.Text = Settings.BOD.ToString("hh:mm:ss");
            dtEoD.Text = Settings.EOD.ToString("hh:mm:ss");
        }

        private void frmOptions_Load(object sender, EventArgs e)
        {
            Console.Write("x");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CommaDelimitedStringCollection updateList = new CommaDelimitedStringCollection();

            for (int i = 0; i < clObj.Items.Count; i++)
                if (clObj.GetItemChecked(i))
                    updateList.Add(clObj.Items[i].ToString());

            if (MessageBox.Show("Are you sure?", "Hey, dude!", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
            {
                UpdateSettings(dtBoD.Value.ToString("hh:mm:ss"), dtEoD.Value.ToString("hh:mm:ss"), txtSpeed.Text.ToString(), updateList.ToString());
            }
            this.SlideDirection = SlideDialog.SlideDialog.SLIDE_DIRECTION.BOTTOM;
            this.Slide();
        }

        private void UpdateSettings(string BoD, string EoD, string interval, string updateList)
        {
            Settings.UpdateSetting("INTERVAL", interval, "AppSettings");
            Settings.TRADING_INTERVAL = Convert.ToInt32(interval);
            txtSpeed.Text = Settings.TRADING_INTERVAL.ToString();

            Settings.UpdateSetting("BoD", BoD, "AppSettings");
            Settings.BOD = Convert.ToDateTime(BoD);
            dtBoD.Text = Settings.BOD.ToString("hh:mm:ss");

            Settings.UpdateSetting("EoD", EoD, "AppSettings");
            Settings.EOD = Convert.ToDateTime(EoD);
            dtEoD.Text = Settings.EOD.ToString("hh:mm:ss");


            Settings.UpdateSetting("UPDATE_LIST", updateList, "AppSettings");
            Settings.UPDATE_LIST = updateList;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to reset to default values?", "Hey, dude!", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
            {
                UpdateSettings("08:00:00", "11:00:00", "1000", string.Empty);

                for (int i = 0; i < clObj.Items.Count; i++)
                    clObj.SetItemChecked(i, false);

                this.SlideDirection = SlideDialog.SlideDialog.SLIDE_DIRECTION.BOTTOM;
                this.Slide();
            }
        }


    }
}
